//
//  XHomeModuleCell.m
//  Bees
//
//  Created by Xiaohan on 2018/12/17.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XHomeModuleCell.h"
#import "UIButton+WebCache.h"
#import "FlatButton.h"
#import "FoundModel.h"
static  NSInteger const kXHomeBtnTag=987;

@interface XHomeModuleCell()
@property (strong, nonatomic) UIScrollView *backScrollView;

@end

@implementation XHomeModuleCell
+ (instancetype)cellForTableView:(UITableView *)tableView{
    return [super cellForTableView:tableView];
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setData:(NSArray<FoundBannerModel*>*)data{
    _data = data;
    CGFloat width = (kScreenWidth-3*kNormalSpace)*0.5f;
    CGFloat height = kXHomeModuleHeight-2*kNormalSpace;
    [data enumerateObjectsUsingBlock:^(FoundBannerModel* obj, NSUInteger idx, BOOL * _Nonnull stop) {
        FlatButton*btn = [self.backScrollView viewWithTag:idx+kXHomeBtnTag];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        if (!btn) {
            btn = [FlatButton button];
            btn.tag = idx+kXHomeBtnTag;
            [self.backScrollView addSubview:btn];
        }
        kWeakSelf(self);
        [btn addTapBlock:^(UIButton * _Nonnull btn) {
            DLog(@"kXHomeBtnTag --- %ld",btn.tag - kXHomeBtnTag);
            !weakself.ModuleClickBlock?:weakself.ModuleClickBlock([weakself.data objectAtIndex:btn.tag - kXHomeBtnTag]);
        }];
        btn.frame =CGRectMake(idx*(width+kNormalSpace), 0, width, height);
        [btn sd_setBackgroundImageWithURL:[NSURL URLWithString:obj.image] forState:UIControlStateNormal];
    }];
    self.backScrollView.contentSize = CGSizeMake(width*data.count+kNormalSpace*(data.count-1), 0);
}

- (void)setupUI{
    UIView*topLine = [UIView new];
    topLine.backgroundColor = kLineColor;
    [self.contentView addSubview:topLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(CGFloatFromPixel(1));
    }];
    UIView*bottomLine = [UIView new];
    bottomLine.backgroundColor = kLineColor;
    [self.contentView addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(CGFloatFromPixel(1));
    }];
    [self.contentView addSubview:self.backScrollView];
    [self.backScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.top.equalTo(topLine.mas_bottom).offset(kNormalSpace);
        make.bottom.equalTo(bottomLine.mas_top).offset(-kNormalSpace);
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - lazy
- (UIScrollView*)backScrollView{
    if (!_backScrollView) {
        _backScrollView = [UIScrollView new];
        _backScrollView.showsVerticalScrollIndicator = NO;
        _backScrollView.showsHorizontalScrollIndicator = NO;
//        _backScrollView.pagingEnabled = YES;
    }
    return _backScrollView;
}

@end
